import axios from 'axios';
// import {useRouter} from 'vue-router'
// const router = useRouter()
// 11
function ajax(options: object) {
    //用promise去封装一下
    return new Promise((resolve, reject) => {
        // 创建一个axios实例
        const axiosIntance = axios.create({
            baseURL: '/api',
            timeout: 5000
        });
        axiosIntance.interceptors.request.use((config: any) => {
            if (localStorage.getItem('token')) {
                let token = localStorage.getItem('token');
                config.headers.Authorization = `Bearer ${token}`;
            }
            return config;
        });
        axiosIntance.interceptors.response.use((response: any) => {
            return response;
        });
        axiosIntance(options)
            .then((res: any) => {
                resolve(res.data);
            })
            .catch((err: any) => {
                if (err.response.status === 401) {
                    alert('登录信息已失效，是否立马前往登录页');
                    window.location.href = '/login';
                }
                reject(err);
            });
    });
}
export default ajax;
